{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Find the Difference of Two Arrays"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: findDifference"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #找出两数组的不同"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个下标从 <code>0</code> 开始的整数数组 <code>nums1</code> 和 <code>nums2</code> ，请你返回一个长度为 <code>2</code> 的列表 <code>answer</code> ，其中：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>answer[0]</code> 是 <code>nums1</code> 中所有<strong> 不 </strong>存在于 <code>nums2</code> 中的 <strong>不同</strong> 整数组成的列表。</li>\n",
    "\t<li><code>answer[1]</code> 是 <code>nums2</code> 中所有<strong> 不 </strong>存在于 <code>nums1</code> 中的 <strong>不同</strong> 整数组成的列表。</li>\n",
    "</ul>\n",
    "\n",
    "<p><strong>注意：</strong>列表中的整数可以按 <strong>任意</strong> 顺序返回。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums1 = [1,2,3], nums2 = [2,4,6]\n",
    "<strong>输出：</strong>[[1,3],[4,6]]\n",
    "<strong>解释：\n",
    "</strong>对于 nums1 ，nums1[1] = 2 出现在 nums2 中下标 0 处，然而 nums1[0] = 1 和 nums1[2] = 3 没有出现在 nums2 中。因此，answer[0] = [1,3]。\n",
    "对于 nums2 ，nums2[0] = 2 出现在 nums1 中下标 1 处，然而 nums2[1] = 4 和 nums2[2] = 6 没有出现在 nums2 中。因此，answer[1] = [4,6]。</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums1 = [1,2,3,3], nums2 = [1,1,2,2]\n",
    "<strong>输出：</strong>[[3],[]]\n",
    "<strong>解释：\n",
    "</strong>对于 nums1 ，nums1[2] 和 nums1[3] 没有出现在 nums2 中。由于 nums1[2] == nums1[3] ，二者的值只需要在 answer[0] 中出现一次，故 answer[0] = [3]。\n",
    "nums2 中的每个整数都在 nums1 中出现，因此，answer[1] = [] 。 \n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums1.length, nums2.length &lt;= 1000</code></li>\n",
    "\t<li><code>-1000 &lt;= nums1[i], nums2[i] &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [find-the-difference-of-two-arrays](https://leetcode.cn/problems/find-the-difference-of-two-arrays/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [find-the-difference-of-two-arrays](https://leetcode.cn/problems/find-the-difference-of-two-arrays/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3]\\n[2,4,6]', '[1,2,3,3]\\n[1,1,2,2]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n",
    "        s1 = set(nums1)\n",
    "        s2 = set(nums2)\n",
    "        return [[i for i in s1 if i not in s2], [i for i in s2 if i not in s1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n",
    "        s1 = set()\n",
    "        s2 = set()\n",
    "        for i in nums1:\n",
    "            if i not in nums2:\n",
    "                s1.add(i)\n",
    "        for j in nums2:\n",
    "            if j not in nums1:\n",
    "                s2.add(j)\n",
    "        return [list(s1),list(s2)]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n",
    "        set1, set2 = set(nums1), set(nums2)\n",
    "        return list(set1-set2), list(set2-set1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n",
    "        num1Set, num2Set = set(nums1), set(nums2)\n",
    "        result = []\n",
    "        temp1 = num1Set - num2Set\n",
    "        temp2 = num2Set - num1Set\n",
    "        result.append(list(temp1))\n",
    "        result.append(list(temp2))\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n",
    "        nums1 = sorted(list(set(nums1)))\n",
    "        nums2 = sorted(list(set(nums2)))\n",
    "        length = min(len(nums1), len(nums2))\n",
    "        print(nums1)\n",
    "        print(nums2)\n",
    "        out = [[],[]]\n",
    "        while nums1 != [] and nums2 != []:\n",
    "            if nums1[0] == nums2[0]:\n",
    "                del(nums1[0])\n",
    "                del(nums2[0])\n",
    "            elif nums1[0] < nums2[0]:\n",
    "                out[0].append(nums1[0])\n",
    "                del(nums1[0])\n",
    "            else:\n",
    "                out[1].append(nums2[0])\n",
    "                del(nums2[0])\n",
    "        out[0] = out[0] + nums1\n",
    "        out[1] = out[1] + nums2\n",
    "\n",
    "        return out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n",
    "        from collections import defaultdict\n",
    "        dicNum = defaultdict(set)\n",
    "        answer = [[],[]]\n",
    "        for i in nums1:\n",
    "            dicNum[i].add(1)\n",
    "        for j in nums2:\n",
    "            dicNum[j].add(2)\n",
    "        print(dicNum)\n",
    "        for k,v in dicNum.items():\n",
    "            if len(v)== 1:\n",
    "                answer[v.pop()-1].append(k)\n",
    "        print(answer)\n",
    "        return answer\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n",
    "        ls1 = []\n",
    "        ls2 = []\n",
    "\n",
    "        for i in nums1:\n",
    "            if i not in nums2 and i not in ls1:\n",
    "                ls1.append(i)\n",
    "        \n",
    "        for j in nums2:\n",
    "            if j not in nums1 and j not in ls2:\n",
    "                ls2.append(j)\n",
    "        \n",
    "        return [ls1, ls2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n",
    "        res = []\n",
    "        trans1 = [num for num in set(nums1) if num not in set(nums2)]\n",
    "        trans2 = [num for num in set(nums2) if num not in set(nums1)]\n",
    "        res.append(trans1)\n",
    "        res.append(trans2)\n",
    "        return res\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def findDifference(self, nums1: List[int], nums2: List[int]) -> List[List[int]]:\n",
    "\n",
    "        return list(set(nums1) - set(nums2)), list(set(nums2) - set(nums1))"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
